package com.guigu.erp.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ModuleFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:StorageCellController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/18 16:31
 * @Author:chenchong
 */
@CrossOrigin
@Controller
@RequestMapping("/StorageCell")
public class StorageCellController {

    @Autowired
    private StorageCellService storageCellService;
    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private NumberUtils numberUtils;

    @ResponseBody
    @RequestMapping("/add")
    public Boolean add(@RequestBody StorageCell cell) {
        //库存配置编号
        String typeNumber = numberUtils.getTypeNumber("400");
        cell.setStoreId(typeNumber);
        //获得分类级别
        QueryWrapper<DesignFile> qw = new QueryWrapper<DesignFile>();
        qw.eq("PRODUCT_ID", cell.getProductId());

        DesignFile one = designFileService.getOne(qw);
        DesignFile d=new DesignFile();
        d.setId(one.getId());
        d.setDesignCellTag("K001-1");
        boolean b1 = designFileService.updateById(d);
        cell.setFirstKindId(one.getFirstKindId());
        cell.setFirstKindName(one.getFirstKindName());
        cell.setSecondKindId(one.getSecondKindId());
        cell.setSecondKindName(one.getSecondKindName());
        cell.setThirdKindId(one.getThirdKindId());
        cell.setThirdKindName(one.getThirdKindName());
        BigDecimal bb=new BigDecimal(0);
        cell.setAmount(bb);
        cell.setCheckTag("S001-0");
        boolean save = storageCellService.save(cell);
        return save;
    }

    @ResponseBody
    @RequestMapping("/show/{currentPage}/{pageSize}")
    public PageUtils<StorageCell> show(  @PathVariable int currentPage,
                                         @PathVariable  int pageSize) {
        QueryWrapper<StorageCell> qw = new QueryWrapper<StorageCell>();
        qw.eq("CHECK_TAG", "S001-0");
        PageHelper.startPage(currentPage, pageSize);
        List<StorageCell> list = storageCellService.list(qw);
        PageInfo<StorageCell> p = new PageInfo<StorageCell>(list);
        PageUtils<StorageCell> utils = new PageUtils<StorageCell>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    @ResponseBody
    @RequestMapping("/StorageCellID")
    public StorageCell StorageCellID(int id) {
        QueryWrapper<StorageCell> qw = new QueryWrapper<StorageCell>();
        qw.eq("id", id);
        StorageCell one = storageCellService.getOne(qw);
        return one;
    }
    @ResponseBody
    @RequestMapping("/getgetOnebianhao")
    public StorageCell getgetOnebianhao(String  productId) {
        QueryWrapper<StorageCell> qw = new QueryWrapper<StorageCell>();
        qw.eq("PRODUCT_ID", productId);
        StorageCell one = storageCellService.getOne(qw);
        return one;
    }

    @ResponseBody
    @RequestMapping("/update")
    public boolean update(@RequestBody  StorageCell cell) {
        cell.setCheckTag("S001-1");
        boolean b = storageCellService.updateById(cell);
        return b;
    }

    @ResponseBody
    @RequestMapping("/showtongguo/{currentPage}/{pageSize}")
    public PageUtils<StorageCell> showtongguo(@RequestBody ModuleFileVo vo,@PathVariable int currentPage,@PathVariable int pageSize) {
        QueryWrapper<StorageCell> qw = new QueryWrapper<StorageCell>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("FIRST_KIND_ID", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("SECOND_KIND_ID", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("THIRD_KIND_ID", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_productName())) {
                qw.eq("PRODUCT_NAME", vo.getSearch_productName());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("REGISTER_TIME", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("REGISTER_TIME", vo.getSearch_registerTime2());
            }
        }
        qw.eq("CHECK_TAG", "S001-1");

        PageHelper.startPage(currentPage, pageSize);
        List<StorageCell> list = storageCellService.list(qw);
        PageInfo<StorageCell> p = new PageInfo<StorageCell>(list);
        PageUtils<StorageCell> utils = new PageUtils<StorageCell>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    @ResponseBody
    @RequestMapping("/showtongguo2")
    public PageUtils<StorageCell> showtongguo2(ModuleFileVo vo,int pageNo, int pageSize) {
        QueryWrapper<StorageCell> qw = new QueryWrapper<StorageCell>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("FIRST_KIND_ID", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("SECOND_KIND_ID", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("THIRD_KIND_ID", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_productName())) {
                qw.eq("PRODUCT_NAME", vo.getSearch_productName());
            }
        }
        qw.eq("CHECK_TAG", "S001-1");

        PageHelper.startPage(pageNo, pageSize);
        List<StorageCell> list = storageCellService.list(qw);
        PageInfo<StorageCell> p = new PageInfo<StorageCell>(list);
        PageUtils<StorageCell> utils = new PageUtils<StorageCell>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @ResponseBody
    @RequestMapping("/productId")
    public Boolean  productId(String productId) {
        QueryWrapper<StorageCell> qw = new QueryWrapper<StorageCell>();
        qw.eq("PRODUCT_ID",productId);
        StorageCell one = storageCellService.getOne(qw);
        if(one==null){
            return false;
        }
        return  one.getCheckTag().equals("S001-1");
    }


}

